
/**
* Recursive field checker with 3 parameters as input
*
* @param  String  orgValue
* @param  String  srcObject
* @param  string  dstObject
*
*/
function chkField3(orgValue, srcObject, dstObject){
  
  var sObject = document.getElementsByName(srcObject);
  sObject = sObject[0];
  
  var dObject = document.getElementsByName(dstObject);
  dObject = dObject[0];
  
  if(dObject.value == "" || 
     dObject.value == " "){
    
    orgValue      = sObject.value;
    sObject.value = "Loading";
    
    setTimeout(chkField3, 500,
               orgValue,
               srcObject,
               dstObject);
  }else{
    
    dObject.value = orgValue;
  }
}

/** 
* Fetch the properties of model based on model name
*
* @param string   mdlName       model name
* @param string   dstModelId    model id
* @param string   dstModelUnit  model unit
* 
*/
function getPropModel(mdlName, dstModelId, dstModelUnit){
	
	var _MODEL_ID   = 0;
	var _MODEL_UNIT = 1;
	var _RET_NULL   = " ";
	var _CONN_ERROR = "****";
	
	dstModelId = document.getElementsByName(dstModelId);
	dstModelId = dstModelId[0];
	
	dstModelUnit = document.getElementsByName(dstModelUnit);
	dstModelUnit = dstModelUnit[0];
	
	var xmlHttp =	new XMLHttpRequest();
	
	if(mdlName.length != ""){
		
		var url 		= "/ecs/tocmenu_41/controllers/getPropModel.php" +"?sid="+Math.random();
		var params 	= "mdlName=" + mdlName;
		
		xmlHttp.open("POST", url, true);
		
		//alert("The requested URL :" + url);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				
				//alert("Responsed :" + xmlHttp.responseText);
				//document.getElementById(cat).innerHTML = xmlHttp.responseText;
				
				/**
				* Check if the return is valid
				* YES: extract the return value
				* NO : ALert the user
				*
				*/
				
				var strChecker = xmlHttp.responseText;
				
				if(strChecker != _RET_NULL){
				  
				  if(strChecker != _CONN_ERROR){
				  
  				  var xString   = strChecker.split("~");
  				  
  				  dstModelId.value   = xString[_MODEL_ID];
  				
  				  dstModelUnit.value = xString[_MODEL_UNIT];
  				}else{
  				  
  				  alert("Oracle connection failed");
  				}
				}else{
				  
				  dstModelId.value   = "Error";
				
				  dstModelUnit.value = "Error";
				}
				
			}
		}
		
		xmlHttp.send(params);
	}
}

/**
* Fetch convertion list based on unit name
*
* @param  string  srcLevelD   
* @param  string  objUnit   
* @param  string  objDestination   
*
*/
function getPropConvertion(srcLevelD, objUnit, objDestination){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  var srcUnit = document.getElementsByName(objUnit);
  srcUnit = srcUnit[0];
  
  var dstObject = document.getElementsByName(objDestination);
  dstObject = dstObject[0];
  
  /**
  * If the unit name and convertion is not null
  *
  */
  if(srcUnit.value != "" ){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/getLstConvertion.php" +"?sid="+Math.random();
		var params 	= "srcUnit="    + srcUnit.value +
		              "&srcLevelD=" + srcLevelD;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  //alert(xmlHttp.responseText);
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			      
			      dstObject.innerHTML = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
  }else{
    
    /**
    * Retry after 1 seconds
    *
    */
    setTimeout(getPropConvertion,2000, 
               srcLevelD, 
               objUnit, 
               objDestination);

  }
}

/** 
* Fetch list of groups by category
*
* @param string   srcCategory   the category selected
* @param string   dstObject     the destinated object
* 
*/
function initLstGroup(srcCategory, dstObject){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  var srcSelection = dstObject.value;
  
  if(srcCategory.length > 0){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/getLstGroup.php" +"?sid="+Math.random();
		var params 	= "srcCategory="    + srcCategory +
		              "&srcSelection="  + srcSelection;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			      
			      dstObject.innerHTML = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
  }
}

/** 
* Fetch list of models by category and group
*
* @param string   srcCategory   the category selected
* @param string   srcGroup      the group selected
* @param string   dstObject     the destinated object
* 
*/
function initLstModel(srcCategory, srcGroup, dstObject){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  srcCategory = document.getElementsByName(srcCategory);
  srcCategory = srcCategory[0].value;
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  var srcSelection = dstObject.value;
  
  if(srcCategory.length > 0 && srcGroup.length > 0){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/getLstModel.php" +"?sid="+Math.random();
		var params 	= "srcCategory="    + srcCategory  +
		              "&srcGroup="      + srcGroup     +
		              "&srcSelection="  + srcSelection;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  
			  //alert(xmlHttp.responseText);
			  
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			      
			      dstObject.innerHTML = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
  }
}

/** 
* Fetch the currency's rate
*
* @param string   srcSymbol   the category selected
* @param string   dstObject   the group selected
* 
*/
function getRate(srcSymbol, dstObject){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  if(srcSymbol.length > 0){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/getRate.php" +"?sid="+Math.random();
		var params 	= "srcSymbol="    + srcSymbol;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  //alert(xmlHttp.responseText);
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			      dstObject.value = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
	}
}

/**
* Calculate the convertion of the quantity selected
*
* @param  string    srcQuantity
* @param  string    srcObject
* @param  string    dstObject
*
*/
function calcConvertion(srcQuantity, srcFrom, srcTo, dstObject){
  
  var _CONN_ERROR = "****";
  var _RET_NULL   = " ";
  
  srcQuantity = document.getElementsByName(srcQuantity);
  srcQuantity = srcQuantity[0];
  
  srcFrom = document.getElementsByName(srcFrom);
  srcFrom = srcFrom[0];
  
  srcTo = document.getElementsByName(srcTo);
  srcTo = srcTo[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  if(srcQuantity.value != "" && 
     srcFrom.value     != "" &&
     srcTo.value       != ""){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/calcConvertion.php" +"?sid="+Math.random();
		var params 	= "srcQuantity="  + srcQuantity.value   +
		              "&srcFrom="     + srcFrom.value       +
		              "&srcTo="       + srcTo.value;
		
		xmlHttp.open("POST", url, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
			
			//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			  //alert(xmlHttp.responseText);
			  switch(xmlHttp.responseText){
			    
			    case _CONN_ERROR:
			      
			      alert("Db connection failed");
			      break;
			      
			    case _RET_NULL:
			      
			      alert("Error has been occured");
			      break;
			      
			    default:
			    
			      dstObject.value = xmlHttp.responseText;
			  }
			}
		}
		
		xmlHttp.send(params);
  }else{
    
    if(srcQuantity.value == ""){
      
      alert("Error: The quantity is NULL");
    }
    
    if(srcFrom.value == ""){
      
      alert("Error: The convertion \"FROM\" is NULL");
    }
    
    if(srcTo.value == ""){
      
      alert("Error: The convertion \"TO\" is NULL");
    }
  }
}

/**
* pass the variable into $_SESSION["passVariables"]
* so that the caller can process the selected check box
* and destroy current window right after 
* this ajax process finish.
*
* @param  string  srcObject
* @param  string  srcJobId
*
*/
function passVariables(srcObject, srcJobId){
  
  /**
  * Check how many checked boxes 
  *
  */
  srcObject   = document.getElementsByName(srcObject);
  
  var passString = "";
  
  var passValue = new Array();
  var passIndex = 0;
  var maxItem   = srcObject.length;
  
  for(iItem = 0; iItem < maxItem; iItem++){
    
    if(srcObject[iItem].checked == true){
      
      passValue[passIndex] = srcObject[iItem].value;
      
      passIndex++;
    }
  }
  
  /**
  * We concat all the array's element
  * into 1 string
  * Since we are about to concat em,
  * we need to add "," but not at the very first element
  * the first element of array is 0th
  *
  */
  for(iElement = 0; iElement < passIndex; iElement++){
    
    if(iElement == 0){
      
      passString = passValue[iElement];
    }else{
      
      passString = passString + "," + passValue[iElement];
    }
  }
  
  /**
  * Process the ajax Call
  *
  */
  if(passString.length > 0){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_41/controllers/passVariables.php" +"?sid="+Math.random();
  	var params 	= "passString="  + passString +
  	              "&passJobId="  + srcJobId;
  	              
  	xmlHttp.open("POST", url, true);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
  		
  		/* Do Nothing */
  		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
  		
  	}
  	
  	xmlHttp.send(params);
  }  
}